package com.zglc.ms.shop.service;

import java.util.HashMap;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import com.zglc.ms.shop.dao.UserDao;
import com.zglc.ms.shop.model.UserInfo;
import com.zglc.ms.shop.util.CommonUtils;

@Service
public class UserService {

	@Autowired
	private UserDao userDao;

	public Integer getUserLevel(int userId) {
		return userDao.getUserLevel(userId);
	}

	public UserInfo getUserInfo(int userId) {
		return userDao.getUserInfo(userId);
	}

	private long lastCacheTime = 0;

	private Map<Integer, String> cacheMap = new HashMap<>();

	public String getLevelName(int level) {
		long nowTime = System.currentTimeMillis();
		if (((lastCacheTime + CommonUtils.MAX_CACHE_TIME_30_MINUTE) > nowTime) && (!CollectionUtils.isEmpty(cacheMap))) {
			if (cacheMap.containsKey(level)) {
				try {
					return cacheMap.get(level);
				} catch (Exception e) {
				}

				return "";
			}
		}

		cacheMap = userDao.getUserLevelMap();
		if (CollectionUtils.isEmpty(cacheMap)) {
			return "";
		}

		lastCacheTime = nowTime;
		if (cacheMap.containsKey(level)) {
			try {
				return cacheMap.get(level);
			} catch (Exception e) {
			}
		}

		return "";
	}

}
